<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
                  <img :src="userinfo.user_pic" alt="" class="avatar" v-if="userinfo.user_pic" />
        <img src="../../assets/logo.png" alt="" class="avatar" v-else />
        <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="mainout"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
          <el-aside width="200px"> <div class="user-box">
        <img :src="userinfo.user_pic" alt="" v-if="userinfo.user_pic" />
        <img src="../../assets/logo.png" alt="" v-else />
        <span>欢迎 {{ userinfo.nickname || userinfo.username }}</span></div>
      <el-menu
      :default-active="$route.path"
       class="el-menu-vertical-demo"
       background-color="#23262E"
      text-color="#fff"
      active-text-color="#409EFF"
      router
      unique-opened>
      <!-- $route 是路由对象  $route.path 动态绑定用户点击的路径-->
      <!-- disabled 禁用的 -->
      <!--  <el-menu-item 没有菜单项   el-submenu 有菜单项-->
        <template v-for="item in menus">
          <!-- 有子菜单 -->
          <el-submenu v-if="item.children" :index="item.indexPath" :key="item.indexPath">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{item.title}}</span>
            </template>
            <el-menu-item v-for="child in item.children" :key="child.indexPath" :index="child.indexPath">
              <i :class="child.icon"></i>
              {{child.title}}
            </el-menu-item>
          </el-submenu>
          <!-- 没有子菜单 -->
          <el-menu-item v-else :index="item.indexPath" :key="item.indexPath">
            <i :class="item.icon"></i>
            <span slot="title">{{item.title}}</span>
          </el-menu-item>
        </template>
       </el-menu>
        </el-aside>
          <el-container>
            <!-- 页面主体区域 -->
            <el-main>
              <!-- 二级路由占位点 -->
              <router-view></router-view>
            </el-main>
            <!-- 底部 footer 区域 -->
            <el-footer>© www.itheima.com - 黑马程序员</el-footer>
          </el-container>
    </el-container>
  </el-container>
</template>

<script>
// 导入vuex，开始渲染页面
import { mapState } from 'vuex'

export default {
  name: 'Main-index',
  data () {
    return {
      menus: []
    }
  },
  methods: {
    mainout () {
      this.$confirm('此操作将永久删除该文件亲', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 退出登录的时候执行的操作
        // 清空token  跳转到登录页去
        // 调用mutations中的方法
        this.$store.commit('user/updatatoken', '')
        this.$router.push('/login')
      }).catch(() => {
        // 捕获到错误消息
      })
    },
    async  getMenus () {
      const { data: res } = await this.$http.get('/my/menus')
      // console.log(res)
      // 存入到data之中
      this.menus = res.data
    }
  },
  created () {
    // 获取左侧的菜单
    this.getMenus()
    // 调用actions中的方法，获取到用户信息
    // 由于是异步操作，调用dispatch方法
    this.$store.dispatch('user/getuserinfo')
  },
  computed: {
    ...mapState('user', ['userinfo'])
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #F2F2F2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
